package com.service.impl;

import com.bean.Power;
import com.bean.Role;
import com.bean.User;
import com.dao.RoleDao;
import com.dao.impl.RoleDaoImpl;
import com.service.RoleService;

import java.util.ArrayList;
import java.util.List;

public class RoleServiceImpl implements RoleService {
    private RoleDao roleDao = new RoleDaoImpl();


    @Override
    public List<Role> getAllRole() {
        return roleDao.getAllRole();
    }

    @Override
    public String[] getAllRoles() {
        List<Role> roleList =new ArrayList<>();
        roleList = roleDao.getAllRole();
        List<String> strings = new ArrayList<>();
        for (Role role:roleList) {
            String roleName = role.getRoleName();
            strings.add(roleName);
        }
        String[] strs =strings.toArray(new String[strings.size()]);
        return strs;
    }

    @Override
    public List<Role> getRoles(User user) {
        return roleDao.getRoles(user);
    }

    @Override
    public boolean addRole(Role role, List<String> powerNames) {
        if(roleDao.addRole(role,powerNames) > 0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public boolean deleteRole(int key) {
        if(roleDao.deleteRole(key) > 0){
            return true;
        }else {
            return false;
        }

    }

    @Override
    public List<Power> showRolePower(int key) {
        return roleDao.showRolePower(key);
    }

    @Override
    public boolean updateRolePower(int key, List<String> powerNames) {
        if(roleDao.updateRolePower(key,powerNames) > 0){
            return true;
        }else {
            return false;
        }

    }

    public String selectRoleName(int key){
        return roleDao.selectRoleName(key);
    };
}
